<?php  include_once("authenticate.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CP2010 MySql Exercise</title>
<link rel="stylesheet" type="text/css" href="style.css"/>

<?php 
	// if user clicked submit button
	if (isset($_REQUEST['Submit'])  )
	{
		//check any record is selected
		if ( !isset($_REQUEST['email']) ) {
			echo "<h3 style='color:red;'>Use radio button to select a record to update.</h3>";
		}
		else {
			// construct a modify query
			$modify_query = "UPDATE colleague SET firstname='" . $_REQUEST['firstName'] . "', lastname='" . $_REQUEST['lastName'] . "', telephone='" .$_REQUEST['telephone'] . "' WHERE email='" . $_REQUEST['email'] . "'";
			//echo $modify_query;  //for debug
		
			// run the query and check result
			$myDB = new db();
			$result = $myDB->sendSQLStatement($modify_query);	
			if ($result == TRUE)
				echo "<h3 style='color:blue;'>Record is modified.</h3>";
			else 
				echo "<h3 style='color:red;'>Fail to modify selected record in database.</h3>";	
			
			// step 4: close the connection
			//close at line 85
	  	}
	}
?>
</head>

<body class="twoColElsLtHdr">

<div id="container">
  <div id="header">
  	<table width="100%" border="0px">
    <tr><td rowspan="2"><h1>Address Book</h1></td>
    <td style="color:#039; font-weight: bold; text-align:right;"><?php	echo 'User: ' . $_SESSION['userEmail']?></td></tr>
    <tr><td style="color:#039; font-weight: bold; text-align:right;"><a href="signBook.php">Open Sign Book</a></td><tr>
    </table>
  <!-- end #header --></div>
  <div id="sidebar1">
     <h3><a href="index.php">Home</a></h3>
    <h3><a href="addAddress.php">Add</a></h3>
    <h3><a href="deleteAddress.php">Delete</a></h3>
  <!-- end #sidebar1 --></div>
  <div id="mainContent">
  	<p>
    <!-- upon the page launch, retrieve all records from table colleague and display -->
    <h2>Select a record to modify </h2>     
	<form action="modifyAddress.php" method="get">  
        <table border="1" cellpadding=="10px" cellspacing="0">
        <tr>
        <th></th>
        <th>Email Address</th>         
        <th>First Name</th>
        <th>Last Name</th>
        <th>Telephone</th>              
        </tr>
        <?php
            $myDB = new db();
	 		$result = $myDB->sendSQLStatement("SELECT * FROM colleague");	
            //loop through all table rows
            while ($row = mysql_fetch_array($result)) {
                echo "<tr >";
                echo "<td><input name='email' type='radio' value='" . $row['email'] . "' /></td>";
                echo "<td cellpadding=\"2px\" >" . $row['email'] . "</td>";				
                echo "<td ><input name='firstName' type='text' value='"  . $row['firstname'] . "' /></td>";
                echo "<td ><input name='lastName' type='text' value='"  . $row['lastname'] . "' /></td>";
                echo "<td ><input name='telephone' type='text' value='"  . $row['telephone'] . "' /></td>";			
                echo "</tr>";										
            }
            // free result memory 
            mysql_free_result($result);										
        ?>
        </table>
    	<p align='center'><input type="submit" name="Submit" value="Submit" /> </p>
    </form>
	<!-- end #mainContent --></div>
	<!-- This clearing element should immediately follow the #mainContent div in order to force the #container div to contain all child floats --><br class="clearfloat" />
   <div id="footer">
    <p>CP2010 MySql Exercise</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>